package com.zelin.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.URI;

/**
 * Created by WF on 2020/8/1 16:48
 */
@Component
@Slf4j
public class MyLogGateWayFilter implements GlobalFilter, Ordered {
    //1.代表过滤器的执行业务
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //1.1)得到请求对象
        ServerHttpRequest request = exchange.getRequest();
        String path = request.getURI().getPath();
        //1.2)判断是否包含有指定的字符串
        if(path.contains("label")){
            log.info("=========>" + path);
            //放行
            return chain.filter(exchange);
            //阻止放行
            //return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }
    //2.代表过滤器的执行顺序
    @Override
    public int getOrder() {
        return 0;
    }
}
